package com.dysoft.flesh.gl;

import com.dysoft.flesh.RenderContext;
import com.dysoft.flesh.Renderer;

import javax.media.opengl.GL;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;

/**
 * @author Sean Micklethwaite
 *         22-Aug-2010
 */
public class GLRenderContext extends RenderContext<GLRenderer> {
	final GL gl;

	public GLRenderContext(GLRenderer renderer, GL gl) {
		super(renderer);
		this.gl = gl;
	}

	@Override
	public void pushTransform() {
		gl.glPushMatrix();
	}

	@Override
	public void popTransform() {
		gl.glPopMatrix();
	}

	@Override
	public void translate(Vector3f v) {
		gl.glTranslatef(v.x, v.y, v.z);
	}

	@Override
	public void transform(Matrix4f m) {
		// Matrix4f is row-major, GL is column-major
		float [] mv = new float [] {
				m.m00, m.m10, m.m20, m.m30,
				m.m01, m.m11, m.m21, m.m31,
				m.m02, m.m12, m.m22, m.m32,
				m.m03, m.m13, m.m23, m.m33,
		};
		gl.glMultMatrixf(mv, 0);
	}

	public GL getGL() {
		return gl;
	}
}
